# python -m project_root.api.api_textalarmset
from .api_token import Token
from util.time_util import timestran
import requests



class Api_TextAlarmSet(Token):


    def get_list(self,stat_time='',end_time='',result='',scene=''):
        url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/sms/list'
        json = {
            'all_region_code':[],
            'limit':10,
            'page':1,
        }
        if stat_time != '':
            json.update(dataRang=[timestran(stat_time).split(' ')[0],timestran(end_time).split(' ')[0]],start_time=stat_time,end_time=end_time)
        if scene != '':
            json.update(scene=scene)
        if result != '':
            json.update(result=result)
        response = requests.post(url=url,headers=self.get_header(),json=json)
        keys = ['send_at','app_scen_name','phone','content','result']
        values = []
        for node in response.json()['data']['list']:
            for key in keys:
                if key == 'send_at':
                    values.append(timestran(node[key]))
                elif key == 'result':
                    if node[key] == 1:
                        values.append('成功')
                    elif node[key] == 2:
                        values.append('失败')
                else:
                    values.append(node[key])
        return values
    

    def get_textset(self):
        url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/sms/config/get'
        response = requests.post(url=url,headers=self.get_header())
        keys = ['sms_alarm_status','sms_alarm_return_status','sms_alarm_policy_limit','sms_alarm_limit']
        values = []
        for key  in keys:
            values.append(response.json()['data'][key])
        return values

